# coding:utf-8
# @Time    : 2021/12/10 8:54
# @Author  : Martin

"""
/nspas-web-in/activityCreateFilter.do?activityType=ORDERACTIVITY&promotionActType=ORDERTRADE&activityChannel=2&comType=1
订单加价购
"""
from page_object.base import PageItems
from util.read_file import get_approve_data


class OrderTradePage(PageItems):

    def __new__(cls, *args, **kwargs):

        instance = super().__new__(cls)
        instance._base_url = f"/nspas-web-in/activityCreateFilter.do?activityType" \
                             f"=ORDERACTIVITY&promotionActType=ORDERTRADE&activityChannel={args[2]}&comType={args[3]}"
        return instance

    def __init__(self, driver, logger, ch_type, com_type):
        super().__init__(driver, logger)
        self.com_type = com_type
        self.ch_type = ch_type

    def input_per_trade_count(self, param):
        """
        输入每单可换购数量
        :param param:
        :return:
        """
        self.input('name', 'limitCondition.perTradeCount', param)

    def input_trade_rule(self, params):
        for param in params:
            index1 = params.index(param) + 1
            # 输入门槛金额
            self.input('css selector',
                       f'#detailinfoOrderTradeRule>table:nth-child({index1}) input[name="orderTradeRule.privilegeSill"]'
                       , param['limit'])
            # 点击添加换购商品
            self.click('css selector', f'#detailinfoOrderTradeRule>table:nth-child({index1}) a[onclick="tradeScopeSet('
                                       f'this)"]')
            for good in param['trade_goods']:
                index2 = param['trade_goods'].index(good) + 1
                self.click('xpath', f'//a[@onclick="tradeScopeAdd(\'{index1}\')"]')
                # 输入商品编码
                self.input('css selector',
                           f'#detailinfoTradeScope{index1}>#detailedtableTradeScope:nth-child({index2}) '
                           f'input[name="activityTradeScope.commodityCode"]', good['code'])
                # 输入商品换购金额
                self.input('css selector',
                           f'#detailinfoTradeScope{index1}>#detailedtableTradeScope:nth-child({index2}) '
                           f'input[name="activityTradeScope.tradeAmount"]', good['price'])
                # 输入商品换购总数量
                self.input('css selector',
                           f'#detailinfoTradeScope{index1}>#detailedtableTradeScope:nth-child({index2}) '
                           f'input[name="activityTradeScope.tradeCount"]', good['count'])
            # 点击确定
            self.click('xpath', f'//a[@onclick="tradeScopeOk(\'{index1}\')"]')
            if params.index(param) != len(params) - 1:
                # 点击增加层级
                self.click('xpath', '//a[@onclick="orderTradeAdd();"]')

    @staticmethod
    def get_approve_head_create():
        """
        获取总部新建审批流账号数据
        :return:
        """
        return get_approve_data('order_trade', 'head', 'create')

